package com.chenyu.upload.schedule;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.chenyu.upload.dao.PictureMapper;
import com.chenyu.upload.entity.Picture;
import com.chenyu.upload.util.HttpUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author yu_chen
 * @date 2018/10/19 14:53
 */
@Component
public class UploadEventSchedule {

    @Resource
    private PictureMapper pictureMapper;

    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 需要刷取的用户ID
     */
    private String[] userIdArr = {"70016888", "70023158", "23697", "69948170", "67072606",
            "70015320", "70016114", "70150057", "70149213", "70149027", "70150665", "70152135",
            "70151807", "70151789", "70151315", "70151703", "70152685", "70153005", "70153405",
            "70154771", "70154765", "70153215", "70156001", "70156385", "70156539"};

    private String groupId = "68397637";

    @Scheduled(fixedRate = 10)
    public void contextLoads() {
        uploadSingle();
    }

    @Scheduled(fixedRate = 100)
    public void contextLoads1() {
        uploadSingle();
    }

    public void uploadSingle() {
        Long id = redisTemplate.opsForValue().increment("id", 1);
        Picture picture = pictureMapper.selectById(id);
        if (picture == null) {
            System.out.println("不存在ID" + id);
            return;
        }
        System.out.println("调用存在ID：" + id);
        pictureMapper.deleteById(id);
        for (String userId : userIdArr) {
            String s = "";
            String pictureString = StringUtils.substringBeforeLast(picture.getName(), "%2C");
            String param = "userid=" + userId + "&groupid=" + groupId + "&content=&picAddress=" + pictureString + "&storage=1500&memorytime=2018-10-17&attentionMember=&publishedTime=&source=%E5%B0%8F%E7%A8%8B%E5%BA%8F&place=&formID=the%20formId%20is%20a%20mock%20one&isPush=true&main=0";
            String url = "https://api.zhuiyinanian.com/YinianProject/event/UploadEvent";
            try {
                s = HttpUtils.sendPost(url, param);
            } catch (Exception e) {
                s = HttpUtils.sendPost(url, param);
            }
            System.out.println(s);
        }

    }
}
